package com.itbuka.order.controller;

import com.itbuka.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.itbuka.order.domain.Order;
import com.itbuka.entity.Result;
import com.itbuka.entity.PageResult;
import com.itbuka.entity.StatusCode;
import com.github.pagehelper.Page;

import java.util.List;
import java.util.Map;

/**
 * 订单
 */
@RestController
@RequestMapping("/Order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 查询全部
     */
    @GetMapping("/selectAll")
    public Result<List<Order>> selectAll() {
        return new Result<>("查询成功", orderService.selectAll());
    }

    /**
     * 条件查询
     */
    @PostMapping("/selectList")
    public Result<List<Order>> select(@RequestBody Order order) {
        return new Result<>("条件查询成功", orderService.selectList(order));
    }

    /**
     * 新增
     */
    @PostMapping("/insert")
    public Result<?> insert(@RequestBody Order order) {
        Integer insert = orderService.insert(order);
        if (insert == -1) {
            return Result.fail("库存不足");
        }
        if (insert == -2) {
            return Result.fail("下单失败");
        }
        if (insert == -3) {
            return Result.fail("减库存失败");
        }
        return Result.ok("新增成功");
    }

    /**
     * 删除
     */
    @GetMapping("/delete")
    public Result<?> delete(@RequestParam String ids) {
        orderService.delete(ids);
        return Result.ok("删除成功");
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result<?> update(@RequestBody Order order) {
        orderService.update(order);
        return Result.ok("修改成功");
    }

    /**
     * 分页搜索实现
     */
    @GetMapping(value = "/pageAll")
    public Result<?> pageAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<Order> pageList = orderService.pageAll(page, size);
        PageResult<?> pageResult = new PageResult<>(pageList.getTotal(), pageList.getResult());
        return new Result<>(true, StatusCode.OK, "查询成功", pageResult);
    }

    /**
     * 分页条件搜索实现
     */
    @GetMapping(value = "/pageList")
    public Result<?> pageList(@RequestBody Order order, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<Order> pageList = orderService.pageList(order, page, size);
        PageResult<?> pageResult = new PageResult<>(pageList.getTotal(), pageList.getResult());
        return new Result<>(true, StatusCode.OK, "查询成功", pageResult);
    }


    /**
     * 购物车下单
     */
    @PostMapping("/addCart")
    public Result addCart(@RequestBody Map map) {
        orderService.addCart(map);
        return Result.ok("购物车下单成功");
    }

    /**
     * 清空购物车
     */
    @PostMapping("/clearCart")
    public Result clearCart(@RequestBody Map map) {
        orderService.clearCart(map);
        return Result.ok("清空购物车成功");
    }

    /**
     * 批量发货
     */
    @PostMapping("/batchSend")
    public Result batchSend(@RequestBody List<Order> orders) {
        orderService.batchSend(orders);
        return Result.ok("发货成功");
    }

    /**
     * 确认收货
     */
    @GetMapping("/take")
    public Result take(@RequestParam String orderId) {
        orderService.confirmTask(orderId);
        return Result.ok("确认收货成功");
    }
}
